//
//  MultiPlatformShare.m
//  golfMaster
//
//  Created by MAC_XXK on 14-6-16.
//  Copyright (c) 2014年 宋鹏程. All rights reserved.
//

#import "ShareSDKHelper.h"
#import "SDImageCache.h"
#import "WXApi.h"
#import "UIImage+Convenience.h"

@implementation ShareSDKHelper

+ (ShareSDKHelper*)sharedMultiPlatform
{
    static dispatch_once_t once = 0;
    static ShareSDKHelper * _sharedMultiPlatform = nil;
    dispatch_once(&once, ^{
        _sharedMultiPlatform = [[ShareSDKHelper alloc] init];
    });
    return _sharedMultiPlatform;
}

// 默认一健分享
// 传入 image 文件
- (void)publishTitle:(NSString *)title andContent:(NSString *)content andImage:(UIImage *)image andUrl:(NSString *)url andDescription:(NSString *)description
{
    // 原先使用 png类型图片
    //[WXApi registerApp:@"wx60b429acaac40f40"];
    CGSize size = CGSizeMake(360.0, 200.0);
    UIImage *tImage=[UIImage scaleAspectFitImage:image ToSize:size];
    id<ISSCAttachment> publishImage = [ShareSDK jpegImageWithImage:tImage quality:0.3];
    id<ISSContent> publishContent = [ShareSDK content:content
                                       defaultContent:nil
                                                image:publishImage
                                                title:title
                                                  url:url
                                          description:description
                                            mediaType:SSPublishContentMediaTypeNews];
    [self publishContent:publishContent];
}


// 传入 image URL
-(void)publishTitle:(NSString *)title andContent:(NSString *)content andImageURL:(NSString *)imageURL andUrl:(NSString *)url andDescription:(NSString *)description
{
    //NSString *imageURL = @"http://image-staging.mastergolf.cn/assets/poster_0faf89c68c3ba0c94b8d28fedbc60194.JPG@1e_720w_405h_1c_0i_1o_50Q_1x.jpg";
    id<ISSCAttachment> publishImageURL = [ShareSDK imageWithUrl:imageURL];
    id<ISSContent> publishContent = [ShareSDK content:content
                                       defaultContent:nil
                                                image:publishImageURL
                                                title:title
                                                  url:url
                                          description:description
                                            mediaType:SSPublishContentMediaTypeNews];
    [self publishContent:publishContent];
}


// 发送图片
-(void)publishImageContent:(UIImage*)image
{
    // 原先使用 png类型图片
    id<ISSCAttachment> publishImage = [ShareSDK jpegImageWithImage:image quality:1.0];
    id<ISSContent> publishContent = [ShareSDK content:nil
                                       defaultContent:nil
                                                image:publishImage
                                                title:@"掌上摄影圈"
                                                  url:nil
                                          description:nil
                                            mediaType:SSPublishContentMediaTypeImage];
    [self publishContent:publishContent];
}


- (void)publishContent:(id<ISSContent>)content{
    NSArray *shareList = [ShareSDK getShareListWithType:
                          ShareTypeWeixiSession,
                          ShareTypeWeixiTimeline,
                          nil];
    [self configShareList:shareList publishContent:content];
}

- (void)configShareList:(NSArray *)shareList publishContent:(id<ISSContent>)content
{
    id<ISSShareOptions> options =
    [ShareSDK defaultShareOptionsWithTitle:nil
                           oneKeyShareList:shareList
                        cameraButtonHidden:YES
                       mentionButtonHidden:NO
                         topicButtonHidden:NO
                            qqButtonHidden:YES
                     wxSessionButtonHidden:YES
                    wxTimelineButtonHidden:YES
                      showKeyboardOnAppear:NO
                         shareViewDelegate:nil
                       friendsViewDelegate:nil
                     picViewerViewDelegate:nil];
    
    [self configShareList:shareList publishContent:content shareOptions:options];
}

//腾讯微博,新浪微博,微信好友,微信朋友圈,邮件,短信的分享，如需要添加新的分享则需要获取对应平台的appKey。
- (void)configShareList:(NSArray *)shareList publishContent:(id<ISSContent>)content
             shareOptions:(id<ISSShareOptions>)options
{
    id<ISSContainer> container = [ShareSDK container];
    
    [ShareSDK showShareActionSheet:container
                         shareList:shareList
                           content:content
                     statusBarTips:NO
                       authOptions:nil
                      shareOptions:options
                            result:^ (ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end)
     {
         
         NSLog(@"error :%@",[error errorDescription]);
         NSLog(@"error code:%ld",(long)[error errorCode]);
         NSLog(@"statusInfo :%@",statusInfo);
         NSString *platformName = [self getSharePlatformName:type];
         NSString *notice = nil;
         if (state == SSPublishContentStateSuccess) {
             notice = [NSString stringWithFormat:@"分享%@成功", platformName];
             //[PCProgressHUD showInView:nil successWithInfo:notice];
         } else if (state == SSPublishContentStateFail) {
            //[PCProgressHUD showInView:nil tipWithInfo:[error errorDescription]];
         }
     }];
}


/***
 *  多平台发送网络图片
 ****/
-(void)sendImageURLContent:(NSString*)url
{
    UIImage *image;
    SSPublishContentMediaType ssPublishContentType;
    
    if (![[SDImageCache sharedImageCache] diskImageExistsWithKey:url])
    {
        ssPublishContentType = SSPublishContentMediaTypeNews;
        image = [UIImage imageNamed:@"AboutLogo"];
    }else
    {
        image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url];
        ssPublishContentType = SSPublishContentMediaTypeImage;
    }

    //发送内容给微信
    id<ISSContent> content = [ShareSDK content:nil
                                defaultContent:nil
                                         image:[ShareSDK jpegImageWithImage:image quality:1]
                                         title:@"佰佳高尔夫分享"
                                           url:url
                                   description:nil
                                     mediaType:SSPublishContentMediaTypeImage];
    
    [self imageShareList:[self getShareList] imageContent:content];
}

/***
 *  多平台发送本地图片
 ****/
-(void)sendImageContent:(UIImage*)image
{
    //发送内容给微信
    id<ISSContent> content = [ShareSDK content:nil
                                defaultContent:nil
                                         image:[ShareSDK jpegImageWithImage:image quality:1]
                                         title:@"佰佳高尔夫分享"
                                           url:nil
                                   description:nil
                                     mediaType:SSPublishContentMediaTypeImage];
    
    [self imageShareList:[self getImageShareList] imageContent:content];
}

-(void)imageShareList:(NSArray*)lists imageContent:(id<ISSContent>)content
{
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    // 平台容器
    id<ISSContainer> container = [ShareSDK container];
    
    [ShareSDK showShareActionSheet:container
                         shareList:lists
                           content:content
                     statusBarTips:YES
                       authOptions:authOptions
                      shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                          
                          NSString *platformName = [self getSharePlatformName:type];

                          NSString *notice = nil;
                          if (state == SSPublishContentStateSuccess) {
                              notice = [NSString stringWithFormat:@"分享%@成功", platformName];
                              //[PCProgressHUD showInView:nil successWithInfo:notice];
                          } else if (state == SSPublishContentStateFail) {
                              //[PCProgressHUD showInView:nil tipWithInfo:[error errorDescription]];
                          }
                      }];
}


// get网络图片分享平台类型
-(NSArray*) getShareList
{
    NSArray *shareList = [ShareSDK getShareListWithType:
                          ShareTypeWeixiSession,
                          ShareTypeWeixiTimeline,
                          nil];
//
//    NSArray *shareList;
//    if ([WXApi isWXAppInstalled]) {
//        if ([QQApi isQQInstalled]) {
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeWeixiSession,
//                         ShareTypeWeixiTimeline,
//                         ShareTypeSinaWeibo,
//                         ShareTypeQQ,
//                         ShareTypeQQSpace,
//                         ShareTypeWeixiFav,
//                         nil];
//        }else{
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeWeixiSession,
//                         ShareTypeWeixiTimeline,
//                         ShareTypeSinaWeibo,
//                         ShareTypeWeixiFav,
//                         nil];
//        }
//    }else{
//        if ([QQApi isQQInstalled]) {
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeSinaWeibo,
//                         ShareTypeQQ,
//                         ShareTypeQQSpace,
//                         nil];
//        }else{
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeSinaWeibo,
//                         nil];
//        }
//    }
////    shareList = [ShareSDK getShareListWithType:
////                          ShareTypeWeixiSession,
////                          ShareTypeWeixiTimeline,
////                          ShareTypeSinaWeibo,
////                          ShareTypeQQ,
////                          ShareTypeQQSpace,
////                          ShareTypeWeixiFav,
////                          //ShareTypeTencentWeibo,
////                          nil];
    return shareList;
}
//
// get本地图片分享平台
-(NSArray*) getImageShareList
{
    NSArray *shareList = [ShareSDK getShareListWithType:
                          ShareTypeWeixiSession,
                          ShareTypeWeixiTimeline,
                          nil];
//
//    //[QQApi isQQInstalled]
//    //[WXApi isWXAppInstalled]
//    NSArray *shareList;
//    if ([WXApi isWXAppInstalled]) {
//        if ([QQApi isQQInstalled]) {
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeWeixiSession,
//                         ShareTypeWeixiTimeline,
//                         ShareTypeSinaWeibo,
//                         ShareTypeQQ,
//                         ShareTypeWeixiFav,
//                         nil];
//        }else{
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeWeixiSession,
//                         ShareTypeWeixiTimeline,
//                         ShareTypeSinaWeibo,
//                         ShareTypeWeixiFav,
//                         nil];
//        }
//    }else{
//        if ([QQApi isQQInstalled]) {
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeSinaWeibo,
//                         ShareTypeQQ,
//                         nil];
//        }else{
//            shareList = [ShareSDK getShareListWithType:
//                         ShareTypeSinaWeibo,
//                         nil];
//        }
//    }
//    
////    shareList = [ShareSDK getShareListWithType:
////                          ShareTypeWeixiSession,
////                          ShareTypeWeixiTimeline,
////                          ShareTypeSinaWeibo,
////                          ShareTypeQQ,
////                          ShareTypeWeixiFav,
////                          //ShareTypeTencentWeibo,
////                          nil];
    return shareList;
}

// get当前分享的平台
-(NSString*)getSharePlatformName:(ShareType)shareType
{
    NSString *platformName = nil;
    switch (shareType)
    {
        case ShareTypeSinaWeibo:
            platformName = @"新浪微博";
            break;
//        case ShareTypeTencentWeibo:
//            platformName = @"腾讯微博";
//            break;
        case ShareTypeQQ:
            platformName = @"QQ";
            break;
        case ShareTypeQQSpace:
            platformName = @"QQ空间";
            break;
        case ShareTypeWeixiSession:
            platformName = @"微信好友";
            break;
        case ShareTypeWeixiTimeline:
            platformName = @"微信朋友圈";
            break;
        case ShareTypeWeixiFav:
            platformName = @"微信收藏";
            break;
        default:
            platformName = @"某个平台";
            break;
    }
    
    return platformName;
}


@end
